package com.ajou.adonate.filemgr;


import java.io.*;
import java.util.*;

public class FileMerge {
	private HashMap<String,String> fileList;
	private byte[] mergeFileByte;
	
	public FileMerge(HashMap<String,String> mergeList) {
		fileList = mergeList;
		
		readFileList();
	}
	
	public void readFileList() {
		int fileSize = 0;
		
		try {
			for (int i=0; i<fileList.size(); i++) {
				File f = new File(fileList.get(""+i));
				
				fileSize += f.length();
			}
			mergeFileByte = new byte[fileSize];
			
			int bytesRead = 0, current = 0;
			for (int i=0; i<fileList.size(); i++) {
				File f = new File(fileList.get(""+i));
				
				FileInputStream fis = new FileInputStream(f);
				BufferedInputStream bin = new BufferedInputStream(fis);
				
				bytesRead =	bin.read(mergeFileByte, current, (mergeFileByte.length-current));
				current += bytesRead;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void mergeFileList(String destPath, String fileName) {
		try {
			File f = new File(destPath, fileName);
			
			FileOutputStream fos = new FileOutputStream(f);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			
			bos.write(mergeFileByte);
			bos.close();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}